package com.cys.dm.rest.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cys.dm.dao.item.DmItemDao;
import com.cys.dm.entity.item.DmItem;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class DmItemService {
    @Resource
    private DmItemDao dmItemDao;

    /**
     * 日历
     * @param itemTypeId
     * @return
     */
    @PostMapping(path = "getCalendarData/{itemTypeId}")
    public List<DmItem> getCalendarData(@PathVariable("itemTypeId")Long itemTypeId){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemType1Id",itemTypeId).groupBy("startTime");
        return dmItemDao.selectList(queryWrapper);
    }
    @PostMapping(path = "getCalendarData2/{itemTypeId}/{startTime}")
    public List<DmItem> getCalendarData2(@PathVariable("itemTypeId")Long itemTypeId,@PathVariable("startTime")String startTime){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",itemTypeId);
        queryWrapper.eq("startTime",startTime);
        return dmItemDao.selectList(queryWrapper);
    }


    /**
     * 根据年龄段分页查询热门商品
     * @param age
     * @param pageSize
     * @return
     */
    @PostMapping(path = {"findDmItemByAgeAndPageSize/{age}/{pageSize}"})
    public List<DmItem> findDmItemByAgeAndPageSize(
            @PathVariable("age")Integer age,
            @PathVariable("pageSize")Integer pageSize
    ){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ageGroup",age);
        queryWrapper.eq("isBanner",1);
        queryWrapper.eq("isRecommend",1);

        IPage<DmItem> page = new Page<>(0,pageSize);

        return dmItemDao.selectPage(page, queryWrapper).getRecords();
    }

    /**
     * 查询热推商品
     * @param itemTypeId 节目类型
     * @param isBanner 是否热推
     * @param isRecommend 是否推荐
     * @return 节目集合
     */
    @PostMapping(path = {"hotItem/{itemTypeId}/{isBanner}/{isRecommend}/{current}/{size}"})
    public List<DmItem> findHotItem(
            @PathVariable("itemTypeId")Long itemTypeId,
            @PathVariable("isBanner")Long isBanner,
            @PathVariable("isRecommend")Long isRecommend,
            @PathVariable("current")Long current,
            @PathVariable("size")Long size
    ){

        IPage<DmItem> page = new Page<>(current,size);

        QueryWrapper<DmItem> wrapper = new QueryWrapper<>();
        wrapper.eq("itemType1Id",itemTypeId );
        wrapper.eq("isBanner",isBanner);
        wrapper.eq("isRecommend",isRecommend);
        return dmItemDao.selectPage(page, wrapper).getRecords();
    }

    /**
     * 热门商品
     * @param itemTyp1Id
     * @param isBanner
     * @param isRecommend
     * @return
     */
    @PostMapping(path = "/hotItemList")
    public List<DmItem> hotItemList(@RequestParam("itemTyp1Id") Long itemTyp1Id,
                                    @RequestParam("isBanner") Long isBanner,
                                    @RequestParam("isRecommend") Long isRecommend){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        IPage<DmItem> page = new Page<>(0,5);
        queryWrapper.eq("itemType1Id",itemTyp1Id);
        queryWrapper.eq("isBanner",isBanner);
        queryWrapper.eq("isRecommend",isRecommend);

        return dmItemDao.selectPage(page,queryWrapper).getRecords();
    }

    /**
     * 今日推荐
     * @return
     */
    @PostMapping(path = "/todayRecommend")
    public List<DmItem> todayRecommend(){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        IPage<DmItem> page = new Page<>(0,6);
        return dmItemDao.selectPage(page,queryWrapper).getRecords();
    }

    /**
     * 即将开售
     * @return
     */
    @PostMapping(path = "/queryToSaleItem")
    public List<DmItem> queryToSaleItem(){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        IPage<DmItem> page = new Page<>(2,6);
        return dmItemDao.selectPage(page,queryWrapper).getRecords();
    }

    /**
     * 首页推荐商品
     * 根据类型以及是否推荐查询
     * @param itemTyp1Id
     * @return
     */
    @PostMapping(path = "/queryFloorItems")
    public List<DmItem> queryFloorItems(@RequestParam("itemTyp1Id")Long itemTyp1Id){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemType1Id",itemTyp1Id);
        queryWrapper.eq("isBanner",1L);
        queryWrapper.eq("isRecommend",1L);
        queryWrapper.last("limit 0,5");
        return dmItemDao.selectList(queryWrapper);
    }

    /**
     * 根据不同分类的推荐剧目
     * 和猜你喜欢公用
     * @param itemTyp1Id
     * @return
     */
    @PostMapping(path = "/postIndexSeniority")
    public List<DmItem> postIndexSeniority(@RequestParam("itemTyp1Id")Long itemTyp1Id){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isRecommend",1L);
        if (itemTyp1Id==0){
            queryWrapper.last("LIMIT 5");
        }else {
            queryWrapper.eq("itemType1Id",itemTyp1Id);
        }
        return dmItemDao.selectList(queryWrapper);
    }

    @PostMapping(path = "findByIdForItem")
    public  DmItem findByIdForItem(@RequestParam("id") Long id){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return dmItemDao.selectOne(queryWrapper);
    }
}
